import Vue from 'vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue'
import VueRouter from "vue-router";
import * as echarts from 'echarts';
import axios from "axios";
import vue2OrgTree from 'vue2-org-tree'
import "vue2-org-tree/dist/style.css"

const orgiinalPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location) {
    return orgiinalPush.call(this, location).catch(err => err)
}
Vue.config.productionTip = false
Vue.use(vue2OrgTree);
Vue.use(ElementUI);
Vue.use(VueRouter)
Vue.prototype.$echarts=echarts

axios.interceptors.request.use(function (rsp) {
    return rsp
}, function (err) {
    return Promise.reject(err)
})
axios.interceptors.response.use(function (rsp) {
    return rsp
}, function (err) {
    return Promise.reject(err)
})
Vue.prototype.$axios = function (info) {
    const method = info.method || 'post';
    const url = info.url;
    const login = this.$loginSession(true);
    const token = login.token || null;
    return {
      then: function(func) {
          axios({
              url: url,
              method: method,
              data: info.data,
              headers: {token: token, 'accept-language': 'zh-CN'}
          }).then((rsp) => {
              if (!rsp.status || rsp.status !== 200) {
                  window.___vue.$message.error(rsp.msg ? rsp.msg: '系统异常')
                  return
              }
              rsp = rsp.data
              if (!rsp.code || rsp.code !== 200) {
                  console.log(Vue)
                  window.___vue.$message.error(rsp.msg ? rsp.msg: '业务异常')
                  return
              }
              func(rsp.content)
          }).catch(error => {
              console.log(error)
              window.___vue.$message.error('系统异常')
          });
      }
    };
}
Vue.prototype.$loginSession = function (noJump) {
    const loginInfo = localStorage.getItem('login')
    if (!loginInfo) {
        if (noJump) {
            return {};
        }
        window.___vue.$message.error('您未登录，跳转登录中...')
        setTimeout(function () {
            this.$router.push({path: '/'})
        }, 2000)
        return
    }
    return JSON.parse(loginInfo)
}

Vue.prototype.$doubleVal = function (v){
    if (!v) {
        return 0
    }
    v = v + ''
    if (v.indexOf('.') > 0) {
        let b = v.split('.')[0];
        let f = v.split('.')[1];
        if (f.length > 2) {
            f = f.substring(0, 2)
        }
        return (b + '.' + f) * 1;
    }
    return v * 1;
}
Vue.prototype.$doubleLab = function (v){
    if (!v) {
        return '0';
    }
    let unite =''
    let limit = 1000
    if(v < 1000) {
        limit = 1
    }else if(v >= 1000 && v < 1000000) {
        unite = 'K'
    }else {
        unite = 'M'
        limit = 1000000
    }
    v = v * 1.0/ limit + '';
    if (v.indexOf('.') > 0) {
        let b = v.split('.')[0];
        let f = v.split('.')[1];
        if (f.length > 2) {
            f = f.substring(0, 2)
        }
        return b + '.' + f + unite;
    }
    return v + unite;
}

Vue.prototype.$getTimeInfo = function (date) {
    const timeLessTen = function (v) {
        if (v <= 9) {
            return '0' + v;
        }
        return v;
    }
    const year = date.getFullYear();
    const month = date.getMonth() + 1; // 月份是从0开始的，所以需要+1
    const day = date.getDate();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    const seconds = date.getSeconds();
    const weekday = date.getDay();
    return {
        toYear: year,
        toMonth: year + '-' + timeLessTen(month),
        toDay: year + '-' + timeLessTen(month) + '-' + timeLessTen(day),
        toHour: year + '-' + timeLessTen(month) + '-' + timeLessTen(day) + ' ' + timeLessTen(hours),
        toMin: year + '-' + timeLessTen(month) + '-' + timeLessTen(day) + ' ' + timeLessTen(hours) + ':' + timeLessTen(minutes) + ':' + timeLessTen(seconds),
      year: year,
      month: timeLessTen(month),
        day: timeLessTen(day),
        hour: timeLessTen(hours),
        minute: timeLessTen(minutes),
        second: timeLessTen(seconds),
        weekOfDay: weekday
    };
}

const routers = [
    {
        path: '/',
        component: () => import("@/components/login-index.vue")
    },
    {
        path: "/fullscreen",
        component: () => import("@/components/fullscreen-index.vue")
    },
    {
        path: "/index",
        redirect: '/index/scan',
        component: () => import("@/components/main-index.vue"),
        children: [
            {
                path: 'scan',
                component: () => import("@/components/info-scan/scan-index.vue"),
            },
            {
                path: 'energy/search/online',
                component: () => import("@/components/energy-search/online-search.vue")
            },
            {
                path: 'energy/search/energy',
                component: () => import("@/components/energy-search/energy-search.vue")
            },
            {
                path: 'energy/search/item',
                component: () => import("@/components/energy-search/item-search.vue")
            },
            {
                path: 'energy/search/department',
                component: () => import("@/components/energy-search/department-search.vue")
            },
            {
                path: 'energy/compare/energy',
                component: () => import("@/components/energy-compare/energy-compare.vue")
            },
            {
                path: 'energy/compare/item',
                component: () => import("@/components/energy-compare/item-compare.vue")
            },
            {
                path: 'energy/listen/diagram_node',
                component: () => import("@/components/energy-listen/diagram_node.vue")
            },
            {
                path: 'energy/listen/warning',
                component: () => import("@/components/energy-listen/warning-manage.vue")
            },
            {
                path: 'energy/listen/subject/map',
                component: () => import("@/components/energy-listen/subject-map.vue")
            },
            {
                path: 'energy/report/energy/excel',
                component: () => import("@/components/energy-report/energy-excel.vue")
            },
            {
                path: 'energy/report/device/excel',
                component: () => import("@/components/energy-report/device-excel.vue")
            },
            {
                path: 'energy/report/energy/report',
                component: () => import("@/components/energy-report/energy-report.vue")
            },
            {
                path: 'system/mrg/institute',
                component: () => import("@/components/system-base/mgr-institute.vue")
            },
            {
                path: 'system/mrg/building',
                component: () => import("@/components/system-base/mgr-building.vue")
            },
            {
                path: 'system/mrg/department',
                component: () => import("@/components/system-base/mgr-department.vue")
            },
            {
                path: 'system/mrg/gateway',
                component: () => import("@/components/system-base/mgr-gateway.vue")
            },
            {
                path: 'system/mrg/item',
                component: () => import("@/components/system-base/mgr-item.vue")
            },
            {
                path: 'system/mrg/meter',
                component: () => import("@/components/system-base/mgr-meter.vue")
            },
            {
                path: 'system/mrg/point',
                component: () => import("@/components/system-base/mgr-point.vue")
            },
            {
                path: 'system/mrg/energy',
                component: () => import("@/components/system-base/mrg-energy.vue")
            },
            {
                path: 'system/mrg/energy/fee',
                component: () => import("@/components/system-base/mrg-energy-fee.vue")
            },
            {
                path: 'system/mrg/diagram/node',
                component: () => import("@/components/dev-ops-mgr/mrg-diagram-node.vue")
            },
            {
                path: 'system/mrg/report/template',
                component: () => import("@/components/system-base/mgr-report-template.vue")
            }
        ]
    }
]
const router = new VueRouter({
    mode: 'hash',
    routes: routers
})

router.beforeEach((to, from, next) => {
    next()
})

window.___vue = new Vue({
    router,
    render: h => h(App),
})
    .$mount('#app')
